class Admin::ArtistsController < Admin
  # GET /artists
  # GET /artists.xml
  def index
    @artists = Artist.find(:all, :include => :region)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @artists }
    end
  end

  # GET /artists/1
  # GET /artists/1.xml
  def show
    @artist = Artist.find(params[:id])
    @page_title = l('common',:viewing) + " " + Artist.name + " " + @artist.fullname
    @latest_artworks = Artwork.find(:all, :conditions => {:artist_id => @artist.id}, :order => :created_at, :limit => 5)
    @medias = Media.find(:all, :conditions => {:artist_id => @artist.id, :artwork_id => nil}, :order => :created_at)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @artist }
    end
  end

  # GET /artists/new
  # GET /artists/new.xml
  def new
    @artist = Artist.new
    
    @page_title = Artist.l_attr(:new)
    @artist_description = ArtistDescription.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @artist }
    end
  end

  # GET /artists/1/edit
  def edit
    @artist = Artist.find(params[:id])

    @page_title = Artist.l_attr(:editing) + " " + Artist.name + " " + @artist.fullname
    @artist_description = @artist.artist_descriptions
    @artist_description_new = ArtistDescription.new
  end

  # POST /artists
  # POST /artists.xml
  def create
    @artist = Artist.new(params[:artist])
    @artist.added_by = current_user.id

    respond_to do |format|
      if @artist.save
        flash[:notice] = 'Artist was successfully created.'
        format.html { redirect_to edit_admin_artist_url(@artist) }
        format.xml  { render :xml => @artist, :status => :created, :location => @artist }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @artist.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /artists/1
  # PUT /artists/1.xml
  def update
    @artist = Artist.find(params[:id])
    @artist.last_edited_by = current_user.id
        
    respond_to do |format|
      if @artist.update_attributes(params[:artist])
        format.html { redirect_to :back }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @artist.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def update_ajax
    @artist = Artist.find(params[:id])
    old_first, old_last, old_region, old_age, old_hometown = @artist.firstname, @artist.lastname, @artist.region_id, @artist.age, @artist.hometown

    respond_to do |format|
      if @artist.update_attributes(params[:artist])
        format.html { redirect_to(@artist) }
        format.xml  { head :ok }
        format.js { render :update do |page|
          if old_first != @artist.firstname
            page.visual_effect :highlight, 'artist_firstname'
          end
          if old_last != @artist.lastname
            page.visual_effect :highlight, 'artist_lastname'            
          end
          if old_age != @artist.age
            page.visual_effect :highlight, 'artist_age'
          end
          if old_hometown != @artist.hometown
            page.visual_effect :highlight, 'artist_hometown'            
          end
          if old_region != @artist.region_id
            page.visual_effect :highlight, 'artist_region_id'
          end
        end }
        
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @artist.errors, :status => :unprocessable_entity }
        # TODO: error
        format.js {}
      end
    end
  end

  # DELETE /artists/1
  # DELETE /artists/1.xml
  def destroy
    @artist = Artist.find(params[:id])
    @artist.destroy

    respond_to do |format|
      format.html { redirect_to(admin_url) }
      format.xml  { head :ok }
    end
  end
 
  def add_description
    @desc = ArtistDescription.new(params[:artist_description])
    @desc.artist_id = params[:artist_id]

    artist = Artist.find_by_id(@desc.artist_id)
    artist.last_edited_by = current_user.id
    artist.save
    
    respond_to do |format|
      if @desc.save
        format.js { render :update do |page|
          page.insert_html :bottom, "artist_description_table", {:partial => "admin/artist_descriptions/edit_description", :object => @desc}
          page.visual_effect :highlight, "artist_description_table", :duration => 2
          page[:artist_description_biography].value = ''
          page[:artist_description_personal_statement].value = ''
          page[:artist_description_language].value = ''
        end }
      else
        # TODO: error
        format.js { render :update do |page|
          page.replace_html "artist_description_new_errors", error_messages_for(:artist_description, :object => @desc)
        end }
        puts "errors: " + @desc.errors.inspect
      end
    end
  end
  
  def delete_description
    @artist_description = ArtistDescription.find(params[:id])
    @artist_description.destroy
    
    respond_to do |format|
      format.js { render :update do |page|
        page.remove "artist_description_#{params[:id]}_row"
        page.visual_effect :highlight, 'artist_description_table', :duration => 2
      end }
    end    
  end
  
  def map
    @artist = Artist.find(params[:id])
    
    @map = GMap.new("map_div")
    @map.control_init(:small_map => true,:map_type => false)
    
    if(@artist.map_lat && @artist.map_lon && @artist.map_zoom) 
      @map.center_zoom_init([@artist.map_lat, @artist.map_lon],@artist.map_zoom)
      @map.overlay_init(GMarker.new([@artist.map_lat,@artist.map_lon],:title => "center"))
    else 
      @map.center_zoom_init([35.46067,2.109375],1)
      @map.overlay_init(GMarker.new([35.46067,2.109375],:title => "center"))
    end
    
    @map.event_init(@map, :move, "function() {
      map.clearOverlays();
      var center = map.getCenter();
      var marker = new GMarker(center);
      map.addOverlay(marker);
    }")
    
    update_form = <<-EOF
    function(){
      var center = map.getCenter();
      var lat = document.getElementById('map_lat');
      var lon = document.getElementById('map_lon');
      var zoom = document.getElementById('map_zoom');
      
      lat.value = center.lat();
      lon.value = center.lng();
      zoom.value = map.getZoom();    
    }
    EOF
    
    # Apparently this includes zoomend too, so we don't need to take that into account right here
    @map.event_init(@map, :moveend, update_form)
  end
  
  def update_map_ajax 
    respond_to do |format|
      format.js { render :update do |page|
        insert_html :before, 'saved', "WOHODS"
        page.show 'saved'
        page.delay(20) do
          page.visual_effect :fade, 'saved'
        end 
      end}
    end
  end
    
  def change_map
    @artist = Artist.find(params[:id])
    @artist.last_edited_by = current_user

    if params[:act] == 'enable'
        @artist.show_map = true
    elsif params[:act] == 'disable'
        @artist.show_map = false
    end
    @artist.save
    
    respond_to do |format|
      format.html { redirect_to :back }
      format.xml  { render :xml => @artist }
    end
  end
  
  def publish
    @artist = Artist.find(params[:id])
    @page_title = l('common', :publishing) + " " + Artist.l_name + " " + @artist.name
    
    respond_to do |format|
      format.html
      format.xml  { render :xml => @artist }
    end
  end

  include AdminHelper  
  def change_published
    @artist = Artist.find(params[:artist])
    @artist.last_edited_by = current_user
    
    if params[:act] == 'publish'
      check_requirements :artist
      if meets_requirements?
        @artist.published = true
        @artist.save
      end
    elsif params[:act] == 'unpublish'
        @artist.published = false
        @artist.save
    end
    
    respond_to do |format|
      format.html { redirect_to :back }
      format.xml  { render :xml => @artist }
    end
  end
end
